package com.xpgk.mvc.bintoStorage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.productOutin.ProductOutin;
import com.xpgk.mvc.productPrice.ProductPrice;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.productStock.ProductStock;
import com.xpgk.mvc.productStock.ProductStockInfo;
import com.xpgk.tools.ToolRandoms;

@Service(name = BintoStorageService.serviceName)
public class BintoStorageService extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BintoStorageService.class);
	public static final String serviceName = "bintoStorageService";	
	

	/**保存入库
	 * 
	 */
	public  String   saveBintoStorage(List<ProductOutin> poList,String  orgids,String  userids,String  bintoDate)
	{	
		
		String msg=ConstantRender.render_success_code;
		
		if(poList.size()>0)
		{
			for(ProductOutin po : poList){
				
				/*保存产品出入库
				 * 
				 */
				String sql=getSqlMy(Product.sqlId_selProByIds);
				Product p=Product.dao.findFirst(sql,po.getStr(ProductOutin.column_productIds),orgids);
				if(p!=null)
				{
					po.set(ProductOutin.column_organIds, orgids)
					.set(ProductOutin.column_date, bintoDate)
					.set(ProductOutin.column_style, "0")
					.set(ProductOutin.column_userIds, userids)
					.set(ProductOutin.column_gxsj, new Date())
					.set(ProductOutin.column_price, p.getFloat(ProductPrice.column_putPrice));
					if(!po.save())
					{
						log.debug("保存产品出入库失败");
						throw new RuntimeException("保存产品出入库失败!");
					}
				}else
				{
					log.debug("产品未设置价格");
					throw new RuntimeException("产品未设置价格!");
				}
				
				//更新产品库存表
				String  sptSql=getSqlMy(ProductStock.sqlId_selPSByOidsAndPids);
				ProductStock  ps=ProductStock.dao.findFirst(sptSql,orgids,po.getStr(ProductOutin.column_productIds));
				if(ps!=null)
				{
					//更新
					float  num=ps.getFloat(ProductStock.column_num)+po.getInt(ProductOutin.column_number);
					ps.set(ProductStock.column_num, num)
						.set(ProductStock.column_gxsj, new Date());
					if(!ps.update())
					{
						log.debug("更新产品总库存失败");
						throw new RuntimeException("更新产品总库存失败!");
					}
					
				}else
				{
					ProductStock  psk=new ProductStock();
					psk.set(ProductStock.column_ids,ToolRandoms.getUuid(true))
						.set(ProductStock.column_organids, orgids)
						.set(ProductStock.column_productID, po.getStr(ProductOutin.column_productIds))
						.set(ProductStock.column_num, (float)po.getInt(ProductOutin.column_number))
						.set(ProductStock.column_gxsj,new Date());
					if(!psk.save())
					{
						log.debug("保存产品总库存失败");
						throw new RuntimeException("保存产品总库存失败!");
					}
					ps=psk;
				}
				//更新库存明细
				SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
		    	String gxsj="%"+dateFormater.format(new Date())+"%";
				String  psiSql=getSqlMy(ProductStockInfo.sqlId_selByOidsAndPidsAndTime);
				ProductStockInfo  psi=ProductStockInfo.dao.findFirst(psiSql,orgids,po.getStr(ProductOutin.column_productIds),gxsj);
				if(psi!=null)
		    	{	
		    		//库存详细信息减少库存
		    		float num=psi.getFloat(ProductStockInfo.column_stock)+po.getInt(ProductOutin.column_number);
		    		psi.set(ProductStockInfo.column_stock,num)
		    			.set(ProductStockInfo.column_gxsj, new Date());
		    		if(!psi.update())
		    		{
		    			log.debug("更新库存明细失败");
						throw new RuntimeException("更新库存明细失败!");
		    		}
		    		
		    	}else
		    	{
		    		ProductStockInfo psInfo=new ProductStockInfo();
		    		String psinfoids = ToolRandoms.getUuid(true);
		    		psInfo.set(ProductStockInfo.column_ids, psinfoids)
		    				.set(ProductStockInfo.column_organids, orgids)
		    				.set(ProductStockInfo.column_productID, po.getStr(ProductOutin.column_productIds))
		    				.set(ProductStockInfo.column_type,po.getStr(ProductOutin.column_typeIds))
		    				.set(ProductStockInfo.column_stock, ps.getFloat(ProductStock.column_num))
		    				.set(ProductStockInfo.column_gxsj, new Date());
		    		if(!psInfo.save())
			    	{
		    			log.debug("保存产品库存记录失败！！");
			    		throw new RuntimeException("保存产品库存记录失败!");
			    	}	
		    	}
			}
		
		} 
		return  msg;
	}
	
	
}
